import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:qaun_ni_dev/im/util/user_info_datesource.dart';
import 'package:qaun_ni_dev/utils/toast.dart';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart'; // 本地存储插件
import 'dart:convert';


// 公共post请求方法 ------------------------------------------------------------------------------
Future requestPost (context,url,{formData}) async {

  var baseUrl = 'http://120.76.60.190:8081/mgr';

  url = baseUrl + url;

  final prefs = await SharedPreferences.getInstance();
  final TokenResult = await prefs.getString('user_token');

  try {
    Response res;
    Dio dio = new Dio();
//    String userToken = 'd24c587b98cd442f9cc59865e1703c29';
    String userToken = TokenResult;
    dio.options.headers = { 'token': userToken };
    if(formData != null) {
//      print('aaaaa');
      res = await dio.post(url,queryParameters: formData);
    } else {
//      print('bbbbb');
      res = await dio.post(url,queryParameters: formData);
    }
    if(res.statusCode == 200) {
//      print('返回数据--->>>>>>${res}');
      return res;
    } else if(res.statusCode == 10001) {
      Toast.toast(context,msg: '登录过期，请重新登录');
      Navigator.pushNamed(context, '/login');
      print('登录过期');
    } else {
      print('请求发生错误${res.statusCode}---${res}');
    }
  } catch (e) {
    Options request;
    print('请求出错了${request}--->>>>>${e}');
  }
}







Future<BaseInfo> getConversionUserInfo(userId) async{
  BaseInfo userDatass = new BaseInfo();

  final prefs = await SharedPreferences.getInstance();
  final TokenResult = await prefs.getString('user_token');

  final userToken = TokenResult;
  try {
    Options options = Options(headers: {"token": userToken});
    int userIdsss = int.parse(userId);
    print(userIdsss is int);
    print(userIdsss is String);
    Response response = await Dio().post(
        "http://120.76.60.190:8081/mgr/appUser/info?uId=${userIdsss}",
        options: options
    );
    var res = json.decode(response.toString());
    print(res);
    userDatass.name = res['data']['nickName'];
    userDatass.portraitUrl = res['data']['avatarUrl'];
    userDatass.id = res['data']['id'].toString();
    print(userDatass.name);
    print(userDatass.portraitUrl);
    print(userDatass.id);
    return userDatass;
  } catch (e) {
    print('获取用户信息发送错误3333333---->>>>${e}');
  }
}





// 公共get请求方法 ------------------------------------------------------------------------------
//Future requestGet (url,{formData}) async {
//  try {
//    Response res;
//    Dio dio = new Dio();
//    String userToken = '999c2e7a9c174aa39d00cec95ab5251f';
//    dio.options.headers = { 'token': userToken };
//
//    if(formData == null) {
//      res = await dio.get(servicePath[url]);
//    } else {
//      res = await dio.get(serviceUrl[url],queryParameters: formData);
//    }
//
//    if(res.statusCode == 200) {
//      return res.data;
//    } else if(res.statusCode == 10001) {
//      print('登录过期');
//    } else {
//      print('请求发生错误${res.statusCode}---${res}');
//    }
//  } catch (e) {
//    print('请求出错了${e}');
//  }
//}